from flask import session

from info import redis_store
from info.models import User
from .import index_blue
import logging
from flask import current_app,render_template
@index_blue.route("/",methods=["GET","POST"])
def hello_world():

    # 获取用户登陆状态
    user_id = session.get("user_id")

    # 获取用户登陆数据‘
    user  = None
    if user_id:
        try:
            user = User.query.get(user_id)
        except Exception as e:
            current_app.logger.error(e)

    # 携带用户数据渲染到页面中
    # user_id = {}
    # if user:
    #     user_dict = {
    #         "id":user_id,
    #         "nick_name":user.nick_name
    #     } 该方法已经在model中封装好to_dict()
    data = {
        # 如果user幼稚，返回左边的值，如果没有则返回右边
        "user_info":user.to_dict() if user else ""  #判断当user为空
    }
    return  render_template("news/index.html",data=data)


# 网站logo处理
@index_blue.route("/favicon.ico")
def web_logo():
    return current_app.send_static_file("news/favicon.ico")